<?php

// 获取设置
function wpjam_get_setting($option, $setting_name){
	if(is_string($option)) $option = wpjam_get_option($option);
	return isset($option[$setting_name])?str_replace("\r\n", "\n", $option[$setting_name]):'';
}

// 获取选项
function wpjam_get_option($option_name){
	$defaults	= wpjam_get_default_option( $option_name );
	$option		= ( is_multisite() && is_network_admin() ) ? get_site_option( $option_name ):get_option( $option_name );
	return wp_parse_args( $option, $defaults);
}

// 获取默认选项
function wpjam_get_default_option($option_name){
	$defaults	= apply_filters($option_name.'_defaults', array());

	if(is_multisite() && !is_network_admin()){
		$site_option	= get_site_option( $option_name );
		$defaults		= wp_parse_args( $site_option, $defaults);
	}

	return $defaults;
}

// 获取当前页面 url
function wpjam_get_current_page_url(){
	$ssl		= (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? true:false;
	$sp			= strtolower($_SERVER['SERVER_PROTOCOL']);
	$protocol	= substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
	$port		= $_SERVER['SERVER_PORT'];
	$port		= ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
	$host		= isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
	return $protocol . '://' . $host . $port . $_SERVER['REQUEST_URI'];
}

// 打印
function wpjam_print_r($value){
	$capability	= (is_multisite())?'manage_site':'manage_options';
	if(current_user_can($capability)){
		echo '<pre>';
		print_r($value);
		echo '</pre>';
	}
}

function wpjam_var_dump($value){
	$capability	= (is_multisite())?'manage_site':'manage_options';
	if(current_user_can($capability)){
		echo '<pre>';
		var_dump($value);
		echo '</pre>';
	}
}